package com.fifth.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Stock;
import com.fifth.order.mapper.StockMapper;
import com.fifth.order.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @作者：刘壬杉
 * @创建时间 2022/9/11 11:05
 **/
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;

    //根据goodsid查询商品库存
    @Override
    public CommonResult getStock(String goodid) {
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodid);
        Stock stock = stockMapper.selectOne(queryWrapper);
        return new CommonResult(2000,"查询成功",stock.getResidue());
    }

    /**
     * 修改库存
     * @param id
     * @param sellnum
     * @param totalnum
     */
    @Override
    public void updateStock(Integer id, Integer sellnum, Integer totalnum) {
        Stock stock = new Stock();
        stock.setTotalNum(totalnum);
        stock.setResidue(totalnum-sellnum);
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",id);
        stockMapper.update(stock,queryWrapper);
    }

    /**
     * 添加库存
     * @param id
     * @param totalnum
     * @param price
     */
    @Override
    public void addStock(Integer id, Integer totalnum, Integer price) {
        Stock stock = new Stock();
        stock.setGoodsId(id.toString());
        stock.setTotalNum(totalnum);
        stock.setSellNum(0);
        stock.setResidue(totalnum);
        stock.setPrice(new BigDecimal(price));
        stockMapper.insert(stock);
    }
}
